/**
 * 
 */
package andersonb.samples;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.TypedQuery;

import andersonb.util.JPAUtil;

/**
 * @author Anderson Bernardo
 * @since 19/05/2013
 */
public class PersistidorDeAutomovel {

	public static void main(String[] args) {
		exclusao();
	}

	public static void exclusao() {
		EntityManager em = JPAUtil.getEntityManager();

		EntityTransaction tx = em.getTransaction();

		Automovel auto = em.getReference(Automovel.class, 3L);

		tx.begin();
		em.remove(auto);
		tx.commit();

	}

	public static void consulta() {
		EntityManager em = JPAUtil.getEntityManager();

		TypedQuery<Automovel> tq = em.createQuery("select a from Automovel a",
				Automovel.class);

		List<Automovel> result = tq.getResultList();

		for (Automovel a : result) {
			System.out.println(a.getMarca());
		}
	}

	public static void insercao() {
		EntityManager em = JPAUtil.getEntityManager();

		Automovel auto = new Automovel();
		auto.setAnoFabricacao(2010);
		auto.setModelo("Ferrari");
		auto.setObservacoes("Nunca foi batido");

		EntityTransaction tx = em.getTransaction();
		tx.begin();
		em.persist(auto);
		tx.commit();

		em.close();
	}
}
